package vlcjdemo;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
/*
 * DelDub.java
 *
 * Created on 13. juni 2005, 22:42
 */

/**
 *
 * @author jtn2
 */
public class ScanMov {

    Map fileList;
    Adler32 adler = new Adler32();
    byte[] tempBuf = new byte[4096];
    Set filetypes = new HashSet();
    FFprobeParser aFFprobeParser;

    /** Creates a new instance of DelDub */
    public ScanMov(Map lfileList, MovDB aMovDB) {
        fileList = lfileList;
        aFFprobeParser = new FFprobeParser();
        aFFprobeParser.aMovDB = aMovDB;
        filetypes.add("mpg");
        filetypes.add("mpeg");
        filetypes.add("avi");
        filetypes.add("riff");
        filetypes.add("wmv");
        filetypes.add("asf");
        filetypes.add("flv");
        filetypes.add("mov");
        filetypes.add("qt");
        filetypes.add("mp4");
        filetypes.add("f4v");
        filetypes.add("rmvb");
        filetypes.add("ram");
        filetypes.add("rm");
        filetypes.add("mkv");
        filetypes.add("divx");
        filetypes.add("asx");
    }

    String getExt(String fileName) {
        String ext = "";
        int mid = fileName.lastIndexOf(".");
        ext = fileName.substring(mid + 1, fileName.length());
        return ext.toLowerCase();
    }

    void scan(File[] dir) {
        for (File file : dir) {
            scanAll(file);
        }
    }
    
    void scanAll(File dir) {
        if (dir.isDirectory()) {
            File[] files;
            files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                if (f.isDirectory()) {
                    scanAll(f);
                } else {
                    scanFile(f);
                }
            }
        } else {
            scanFile(dir);
        }
    }

    void scanFile(File f) {
        String ext = getExt(f.getName());

        if (filetypes.contains(ext)) {
            adler.reset();
            try {
                // Compute Adler-32 checksum
                CheckedInputStream cis = new CheckedInputStream(new BufferedInputStream(new FileInputStream(f)), adler);
                while (cis.read(tempBuf) >= 0) {
                }
                Long checksum = new Long(cis.getChecksum().getValue());
                cis.close();
                Mov mov = (Mov) fileList.get(checksum);
                if (mov == null) {
                    mov = new Mov(checksum);
                    fileList.put(checksum, mov);
                    Set filenames = new HashSet();
                    filenames.add(f.getCanonicalPath());
                    mov.info.put("filename", filenames);
                    mov.info.put("filesize", f.length());
                    aFFprobeParser.writeFFProbeCmdMov(f.getCanonicalPath(), mov);
                    aFFprobeParser.writeJpgCmdMov(f.getCanonicalPath(), mov);
                } else {
                    Set filenames = (Set) mov.info.get("filename");
                    filenames.add(f.getCanonicalPath());
                    String theUrl = aFFprobeParser.aMovDB.curDir + "img" + File.separator + "MovDB_" + (Long) mov.info.get("Checksum") + ".jpg";
                    File imgf = new File(theUrl);
                    if (!imgf.exists()) {
                       aFFprobeParser.writeJpgCmdMov(f.getCanonicalPath(), mov);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
